import { menu } from "../../decorator";

const { ccclass } = cc._decorator;

@ccclass
@menu(__filename)
export default class touchSingle extends cc.Component {
    private _touchID: number = null;

    private _touchStart(event) {
        if (this._touchID !== null) {
            event.stopPropagation();
        } else {
            this._touchID = event.getID();
        }
    }

    private _touchMove(event) {
        if (this._touchID !== event.getID()) {
            event.stopPropagation();
        }
    }

    private _touchEnd(event) {
        if (this._touchID !== event.getID()) {
            event.stopPropagation();
        } else if (!event.simulate) {
            this._touchID = null;
        }
    }

    onEnable() {
        this.node.on(cc.Node.EventType.TOUCH_START, this._touchStart, this, true);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMove, this, true);
        this.node.on(cc.Node.EventType.TOUCH_END, this._touchEnd, this, true);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchEnd, this, true);
    }

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_START, this._touchStart, this, true);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._touchMove, this, true);
        this.node.off(cc.Node.EventType.TOUCH_END, this._touchEnd, this, true);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._touchEnd, this, true);
    }
}
